package com.lixin.smartclassroom.ui

import android.app.Activity
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.Canvas
import android.net.Uri
import android.text.Layout
import android.text.StaticLayout
import android.text.TextPaint
import android.view.Gravity
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.bumptech.glide.Glide
import com.drake.channel.receiveEvent
import com.drake.channel.sendEvent
import com.drake.net.utils.scopeNetLife
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import com.lixin.smartclassroom.R
import com.lixin.smartclassroom.SmartClassroomApp
import com.lixin.smartclassroom.api.getmessagecount
import com.lixin.smartclassroom.databinding.ActivityMainBinding
import com.lixin.smartclassroom.ui.home.tab1.fragment.Tab1Fragment
import com.lixin.smartclassroom.ui.home.tab2.fragment.Tab2Fragment
import com.lixin.smartclassroom.ui.home.tab3.fragment.Tab3Fragment
import com.lixin.smartclassroom.ui.home.tab4.fragment.Tab4Fragment
import com.lixin.smartclassroom.ui.home.tab5.fragment.Tab5Fragment
import com.lixin.smartclassroom.utils.LoginManager
import com.lixinkeji.base.BaseActivity
import com.lixinkeji.base.RxManager
import com.lixinkeji.extension.GSON
import com.lixinkeji.extension.addIntArgument
import com.lixinkeji.extension.commonDialog
import com.lixinkeji.extension.getIntentInt
import com.lixinkeji.extension.hasNotificationManager
import com.lixinkeji.extension.loadCircle
import com.lixinkeji.extension.onClick
import com.lixinkeji.extension.openNotificationManager
import com.lixinkeji.extension.requestPerm
import com.lixinkeji.extension.start
import com.lixinkeji.extension.toast
import com.lixinkeji.extension.uId
import com.lixinkeji.utils.BitmapUtil
import com.lixinkeji.utils.GlideEngine
import com.luck.picture.lib.basic.PictureSelector
import com.luck.picture.lib.config.SelectMimeType
import com.luck.picture.lib.config.SelectModeConfig
import com.luck.picture.lib.entity.LocalMedia
import com.luck.picture.lib.interfaces.OnResultCallbackListener
import io.reactivex.functions.Consumer

class MainActivity : BaseActivity<ActivityMainBinding>() {
    override fun topBarHide() = true
    override fun isCanExit() = true
    override fun startPadding() = false
    var tabIndex = 0
    var localMedia: LocalMedia? = null
    var view: View? = null


    override fun needRegisterNetworkChangeObserver() = true


    override fun iniView() {
        getMessageCount()
//        if (!hasNotificationManager()) {
//            commonDialog("开启通知权限",
//                content = "在设置中开启消息通知权限，及时获取新消息通知",
//                cancel = "取消",
//                sure = "确定",
//                onSure = {
//                    openNotificationManager()
//                })
//        }

        tabIndex = getIntentInt("tabIndex", 0)
        localMedia = intent.getParcelableExtra("localMediaList")

        binding.appTabBar.apply {
            add(Tab1Fragment())
            add(Tab2Fragment())
            add(Tab3Fragment())
            add(Tab4Fragment())
            add(Tab5Fragment())
            commit(supportFragmentManager, R.id.container)
        }

        val rxManager = RxManager()
        rxManager.on("tokenException", Consumer<String> {
            toast("您的登录已过期，请重新登录")
            LoginManager.logout(this@MainActivity)
        })


    }

    override fun onResume() {
        super.onResume()
        getMessageCount()
    }

    private fun getMessageCount() {
        scopeNetLife {
            val baseBean = getmessagecount()
            if (baseBean.result == "0") {
                if (baseBean.count?.isNotEmpty() == true && baseBean.count != "0")
                    sendEvent(baseBean.count.toString(), "showMessageCount")
            } else if (baseBean.result == "-2") {
                toast(baseBean.resultNote.toString())
                LoginManager.logout(this@MainActivity)
            } else {
                toast(baseBean.resultNote.toString())
            }
        }
    }


    override fun getViewBinding() = ActivityMainBinding.inflate(layoutInflater)


}